<?php
/**
 *
 * Author:苏光盼
 * Date:2020/6/21 下午10:39
 * Class:RoleMenu.php
 * Web:https://app.admin.suanzisu.com
 * Project:通用APP后台管理系统
 *
 */

namespace App\Logic;


use App\Model\GroupMenuMap;
use App\Model\RoleMenu as Rmenu;
use App\Util\Common;
use EasySwoole\Http\Request;
use EasySwoole\ORM\Exception\Exception;


final class RoleMenu
{
    /**
     * @param Request $req
     *
     * @param int $l
     *
     * @return array
     * @throws \Throwable 以列表的形式 列出后台用户组的权限菜单 带分页
     */
    public static function list( Request $req, $l = 0)
    {
        $limit = Common::limit($req);
        try {
            if($l > 0){
                $limit[1] = $l;
            }
            $mode = Rmenu::create()->withTotalCount()->limit(...$limit);
            if($group_id = (int)$req->getQueryParam('group_id') )
            {
                $mode->alias('rm')->join('group_menu_map map','map.role_menu_id = rm.id','INNER')
                    ->where('map.group_id',$group_id )
                    ->field('rm.id,map.id as map_id,
                        rm.title,rm.pid,rm.href,map.ord,rm.icon')->order('map.ord');
            }
            $data = $mode->all();
            $result = $mode->lastQueryResult();
            $rows = $result->getTotalCount();
            return ['data'=>$data,'rows'=>$rows];
        }  catch (\Throwable $e) {
            throw $e;
        }

    }

    /**
     * @param array $data
     *
     * @return bool|int|null
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \Throwable
     *
     * 保存修改或新增的权限菜单
     */
    public static function updateOrInsert( array $data)
    {
        $insertModel = Rmenu::create();
        if(isset($data['id']) && (int)$data['id'] > 0)
        {
            $mode = Rmenu::create()->get($data['id']);
            $ret = $mode->update($data);
        }
        else
        {
            $ret = $insertModel->data($data)->save();
        }
        return $ret;
    }


    /**
     * @param int $group_id
     *
     * @return array
     * @throws Exception
     * @throws \Throwable
     *
     * 获取某个用户组的权限列表
     */
    public static function findGroupMenu(  int $group_id) : array
    {
        $ret = [];
        $mode = GroupMenuMap::create()->field('id,role_menu_id')->order('ord')
                ->with('role_menu')->where('group_id',$group_id)->all();
        if($mode){
            foreach($mode as $k => $v){
                if($v->role_menu) $ret[$k] = $v->role_menu->toArray();
            }
        }
        return $ret;

    }


    /**
     * @return array|bool|mixed|null
     * @throws Exception
     * @throws \Throwable
     *
     * 以树的形式 列出所有的后台用户组权限菜单
     */
    public static function findByTree()
    {
        $ret = [];
        $data = Rmenu::create()->all();
        if($data){
            foreach($data as $k => $v){
                $ret[$k] = $v->toArray();
            }
        }
        return Common::create_menu($ret,'children');
    }
}